package com.rqf.ls.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.rqf.ls.service.ICommonDataService;
import com.rqf.ls.service.IRegisterOfLawyerService;
import com.rqf.ls.service.IRegisterService;
import com.server.password.MyMD5;
import com.server.web.DataVector;
import com.server.web.RequestInput;

@Service("RegisterOfLawyerService")
public class RegisterOfLawyerService implements IRegisterOfLawyerService {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private IRegisterService registerService;
	@Autowired
	private ICommonDataService commonDataService;
	
	
	 /**
	 * 检查用户是否已注册
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Boolean checkPhone(String phoneNo){
		String sql = "Select count(Id) from lsuser where Phone='"+phoneNo+"'";
		int count = jdbcTemplate.queryForInt(sql);
		if (count > 0) {
			return false;
		}else {
			return true;
		}
	}
	/**
	 * 向手机发送验证码
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public String sendCode(String phoneNo){
		String sendCode = registerService.sendCode(phoneNo);
		return sendCode;
	}
	
	 /**
	 * 增加用户
	 * @param userMap
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Boolean add(Map<String, Object> userMap){
		Boolean isSuccess = true;
		Object phoneObj = userMap.get("phoneNo");
		Object pswObj = userMap.get("password");
		if (phoneObj == null || pswObj == null) {
			isSuccess = false;
			return isSuccess;
		}
		//MD5经过3次加密
		String encryptStr = MyMD5.circularlyEncrypt(pswObj.toString(), 3);
		
		// 添加用户
		String sql = "insert into lsuser (id,REGISTTIME,phone,NICKNAME，userPwd,PHONECHECKED,ABLE,fuwutypeid) values(lsuser_sequence.nextval,SYSDATE,'%s','%s','%s',1,1,1)";
		try {
			jdbcTemplate.execute(String.format(sql, phoneObj.toString(),phoneObj.toString(),encryptStr.toString()));
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	 /**
	 * 个人资料编辑
	 * @param phoneNo
	 * @param userMap
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Boolean update(String phoneNo,Map<String, Object> userMap){
		Boolean isSuccess = true;
		StringBuffer sqlBuffer = new StringBuffer();
		sqlBuffer.append("Update lsuser set ");
		Set<String> keySet = userMap.keySet();
		int index = 0;
		for(String key : keySet){
			if (index != 0) {
				sqlBuffer.append(",");
			}
			sqlBuffer.append(key).append("='").append(userMap.get(key)).append("'");
			index ++;
		}
		sqlBuffer.append(" where phone='").append(phoneNo).append("'");
		try {
			jdbcTemplate.execute(sqlBuffer.toString());
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	@Override
	public Boolean update(String phoneNo,String field,String value){
		Boolean isSuccess = true;
		String sql = "Update lsuser set %s='%s where phone='%s'";
		try {
			jdbcTemplate.execute(String.format(sql, field, value, phoneNo));
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	 /**
	 * 获取律师信息
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Map<String, Object> read(String phoneNo){
		String sql = "Select * from lsuser where phone='%s' and able=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, phoneNo));
		if (list.size() > 0) {
			return list.get(0);
		}else {
			return new HashMap<String, Object>();
		}
	}
	
	@Override
	public Map<String, Object> readEvaluate(String phoneNo){
		String sql = "Select * from LAWYEREVA where LAWYERPHONE='%s'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String.format(sql, phoneNo));
		double eva01 = 0.00;
		double eva02 = 0.00;
		double eva03 = 0.00;
		int sumEva01 = 0;
		int sumEva02 = 0;
		int sumEva03 = 0;
		Map<String, Object> outMap = new HashMap<String, Object>();
		if (list.size() > 0) {
			for (Map<String, Object> map : list) {
				sumEva01 += Integer.parseInt(map.get("EVA01").toString());
				sumEva02 += Integer.parseInt(map.get("EVA02").toString());
				sumEva03 += Integer.parseInt(map.get("EVA03").toString());
			}
			eva01 = ((double)sumEva01)/list.size();
			eva02 = ((double)sumEva02)/list.size();
			eva03 = ((double)sumEva03)/list.size();
		}
		outMap.put("EVA01", eva01);
		outMap.put("EVA02", eva02);
		outMap.put("EVA03", eva03);
		return outMap;
	}
	@Override
	public Object read(String phoneNo, String field){
		String sql = "Select " + field +" from lsuser where phone='"
				+phoneNo+"' and able=1";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		if (list.size() > 0) {
			return list.get(0).get(field);
		}else {
			return null;
		}
	}
	 /**
	 * 修改密码
	 * @param phoneNo
	 * @param password
	 * @return
	 * @author ServerZhang
	 * @date 2016年3月28日
	 */
	@Override
	public Boolean updatePswd(String phoneNo,String password){
		String encrypt = MyMD5.circularlyEncrypt(password, 3);
		return this.update(phoneNo, "userPwd", encrypt);
	}
	/**
	 * 获取用户头像路径
	 * @param phoneNo
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月10日
	 */
	@Override
	public String getPhotoPath(String phoneNo) {
		String path = "";
		String sql = "Select * from ATTACHMENT where saveType='LawyerPhoto' %s order by createTime desc";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(String
				.format(sql, "and phoneNo='" + phoneNo +"'"));
		if (list.size() > 0) {
			Map<String, Object> map = list.get(0);
			Object pathObj = map.get("FILEPATH");
			Object uuidObj = map.get("UUID");
			Object typeObj = map.get("TYPE");
			if (pathObj != null && uuidObj != null && typeObj != null) {
				path += pathObj.toString() +"\\" + uuidObj.toString()
						+"."+ typeObj.toString();
			}
		}
		return path;
	}

	/**
	 * 所有用户列表
	 * 
	 * @param requestInput
	 * @param type
	 *            0、正常用户；1、黑名单用户；其他为所有用户
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月16日
	 */
	@Override
	public DataVector<Map<String, Object>> userList(RequestInput requestInput,
			Integer inBlackList) {
		String table = "LSUSER";
		String _whereStr = "CHECKED=1";
		String sortStr = "REGISTTIME DESC";
		if (inBlackList != null) {
			switch (inBlackList) {
			case 0:
				_whereStr += " and INBLACKLIST=0";
				break;
			case 1:
				_whereStr += " and INBLACKLIST=1";
				break;

			default:
				break;
			}
		}
		DataVector<Map<String, Object>> listData = commonDataService
				.getListData(table, _whereStr, sortStr, requestInput);
		return listData;
	}
	/**
	 * 待审核律师列表
	 * @param requestInput
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月16日
	 */
	@Override
	public DataVector<Map<String, Object>> checkingList(RequestInput requestInput) {
		String table = "LSUSER";
		String _whereStr = "CHECKED=0";
		String sortStr = "REGISTTIME DESC";
		DataVector<Map<String, Object>> listData = commonDataService
				.getListData(table, _whereStr, sortStr, requestInput);
		return listData;
	}
	 /**
	 * 拉黑、解除拉黑
	 * @param phoneNo
	 * @param inBlackList
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月17日
	 */
	@Override
	public Boolean pullToBlack(String phoneNos,Integer inBlackList){
		String phoneStrs = "'"+phoneNos.replace(",", "','")+"'";
		Boolean isSuccess = true;
		String sql = "Update lsuser set INBLACKLIST="+inBlackList+" where phone in("+phoneStrs+")";
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	/**
	 * 审核律师
	 * @param phoneNo
	 * @param inBlackList
	 * @return
	 * @author ServerZhang
	 * @date 2016年5月17日
	 */
	@Override
	public Boolean check(String phoneNo,Integer checked){
		Boolean isSuccess = true;
		String sql = "Update lsuser set CHECKED="+checked+" where phone='"+phoneNo+"'";
		try {
			jdbcTemplate.execute(sql);
		} catch (Exception e) {
			isSuccess = false;
		}
		return isSuccess;
	}
	
	
}
